package com.example.demo.generator.scaffold.controller;

import com.example.demo.common.BaseResponse;
import com.example.demo.common.ResultUtils;
import com.example.demo.generator.scaffold.dto.stats.ScaffoldStatsQueryRequest;
import com.example.demo.generator.scaffold.dto.stats.ScaffoldStatsResponse;
import com.example.demo.generator.scaffold.service.ScaffoldGenerationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 脚手架使用统计接口。
 */
@RestController
@RequestMapping("/generator/scaffold/stats")
@RequiredArgsConstructor
@Validated
@ConditionalOnProperty(prefix = "feature.database", name = "enabled", havingValue = "true")
@Tag(name = "脚手架统计", description = "统计脚手架生成使用情况")
public class ScaffoldStatsController {

    private final ScaffoldGenerationLogService logService;

    @PostMapping("/query")
    @PreAuthorize("hasRole('ADMIN')")
    @Operation(summary = "多维度统计查询", description = "根据维度与筛选条件统计 ZIP 生成情况")
    public BaseResponse<ScaffoldStatsResponse> query(@Valid @RequestBody ScaffoldStatsQueryRequest request) {
        return ResultUtils.success(logService.queryStats(request));
    }
}
